var vm_sonar_profile = new Vue({
  el: ".sonar_profile",
  delimiters: ["[[", "]]"],
  data: {
    btn_msg: "提交更新",
    d01: "linux",
    sonar_profiles: [],
    sonar_profiles_original: [],
    sorted_item: "Profile_Name",
    sort_direction: "up",
    search_keywords: [],
    search_input: "",
    kw_index: 999,
    re_data: /^\d+$/,
    input_alert_show: false,
  },
  created() {},
  destroyed() {},
  mounted() {
    this.init_setup();
  },
  methods: {
    filtering(operation) {
      if (operation == "add_kw") {
        for (kw of this.search_keywords) {
          if (this.re_data.test(kw)) {
            let temp_data = this.sonar_profiles.filter((profile, index, arr) => {
              for (item in profile) {
                if (profile[item] - 4 < kw && kw <= profile[item] + 4) {
                  return true;
                }
              }
              return false;
            });
            this.sonar_profiles = temp_data;
          } else {
            let temp_kw = kw.toLowerCase();
            this.sonar_profiles = this.sonar_profiles.filter((profile, index, arr) => {
              temp_name = profile["Profile_Name"].toLowerCase();
              if (temp_name.includes(temp_kw)) {
                return true;
              }
              return false;
            });
          }
        }
      } else {
        if (this.search_keywords.length == 0) {
          this.sonar_profiles = this.sonar_profiles_original;
          return;
        }
        for (kw of this.search_keywords) {
          if (this.re_data.test(kw)) {
            this.sonar_profiles = this.sonar_profiles_original.filter((profile, index, arr) => {
              for (item in profile) {
                if (profile[item] - 4 < kw && kw <= profile[item] + 4) {
                  return true;
                }
              }
              return false;
            });
          } else {
            let temp_kw = kw.toLowerCase();
            this.sonar_profiles = this.sonar_profiles_original.filter((profile, index, arr) => {
              temp_name = profile["Profile_Name"].toLowerCase();
              if (temp_name.includes(temp_kw)) {
                return true;
              }
              return false;
            });
          }
        }
      }
    },
    delete_kw(kw) {
      this.search_keywords = this.search_keywords.filter((v, i, arr) => {
        return v != kw;
      });
      this.filtering("del_kw");
      this.input_alert_show = false;
    },
    clear_search_kw() {
      this.search_keywords = [];
      this.search_input = "";
      this.sonar_profiles = this.sonar_profiles_original;
    },
    update_search_kw() {
      this.input_alert_show = false;
      if (this.search_input) {
        if (!this.search_keywords.includes(this.search_input)) {
          if (this.search_input > 9999) {
            this.input_alert_show = true;
            return;
          }
          this.search_keywords.push(this.search_input);
          this.filtering("add_kw");
        }
      }
    },
    sortByKey(array, key, direction) {
      if (direction == "up") {
        return array.sort(function (a, b) {
          var x = a[key];
          var y = b[key];
          return x < y ? -1 : x > y ? 1 : 0;
        });
      } else {
        return array.sort(function (a, b) {
          var x = a[key];
          var y = b[key];
          return x < y ? (x > y ? 0 : 1) : -1;
        });
      }
    },
    sort(e) {
      // console.log('-------------------------------------');
      // console.log(e);
      // console.log('-------------------------------------');
      if (this.sorted_item != e) {
        this.sorted_item = e;
      } else {
        this.sort_direction = this.sort_direction == "up" ? "down" : "up";
      }
      this.sortByKey(this.sonar_profiles, e, this.sort_direction);
    },
    async axiosGet(url, params) {
      try {
        const response = await axios.get(url, { params });
        return response.data;
      } catch (err) {
        console.log(err.response.status);
      }
    },
    async init_setup() {
      let res = await this.axiosGet("/sonar-api/dataTable/");
      this.sonar_profiles = res.results;
      this.sonar_profiles_original = res.results;
      // console.log('-------------------------------------');
      // console.log(this.sonar_profiles.slice(1));
      // console.log('-------------------------------------');
    },
  },
  watch: {},
});
